package com.webservice;

import com.barecodes.Ean18Generator;
import com.carpark.ejb.DAO.DAO;
import com.carpark.ejb.Model.AccountingdataModel;
import com.carpark.ejb.Model.CarsModel;
import com.carpark.ejb.Model.ClientsModel;
import com.carpark.ejb.Model.ParkingpassesModel;
import com.carpark.ejb.Model.ParkingspacesModel;
import com.carpark.ejb.Model.PassesrecordsModel;
import com.carpark.ejb.Model.ReceivablesModel;
import com.carpark.ejb.Model.TarrifsModel;
import com.itextpdf.text.DocumentException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.event.FlowEvent;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import javax.annotation.PostConstruct;
import javax.faces.application.Application;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;
import javax.servlet.ServletContext;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

/**
 *
 * @author Krzysztof
 */
@ViewScoped
@ManagedBean
public class UserWizard implements Serializable {

    private final DAO dao = getSingleDao();
    DaoSingleObject dso;
    private Date startDate;
    private String startDateForUser;
    private Date endDate;
    private String endDateForUser;
    private String carType;
    private String spaceType;
    private List<ParkingspacesModel> avalivableSpaces;
    private List<CarsModel> carsByClient;
    private ParkingspacesModel selectedSpace;
    private AccountingdataModel selectedAcc;
    private CarsModel selectedCar;
    private Integer choosenCarId;
    private ClientsModel currentUser;
    private LoginBean lg;
    private String radioOptions;
    private String radioOptionsACC;
    private static Logger logger = Logger.getLogger(UserWizard.class.getName());
    public boolean takeDataFromClientTable;
    private String accName;
    private String accCity;
    private String accZip;
    private String accStreet;
    private String accHouse;
    private String accLocal;
    private String accNip;
    private String accPhone;
    private String accPayerType;
    private String carPlate;
    private String carModel;
    private String carMake;
    private String carTypeForUser;
    private String spaceTypeForUser;
    private String AccMobile;
    private String payerTypeForUser;
    private CarsModel addedCar;
    private AccountingdataModel addedAccountingData;
    SelectItem[] carsByClientOptions;
    private boolean isSucces;
    private boolean mobilePreRenderEnabler;
    private String radioOptionsACC_mobile;
    SimpleDateFormat formatter;
    private File fileToDownload;
    private File fileToDownloadM;
    StreamedContent fileDM;

    @PostConstruct
    public void init() {

        setCurrentUser(dao.getClientById(getUserID()));
        formatter = new SimpleDateFormat("EEE d MMM yy", new Locale.Builder().setLanguage("pl").setRegion("PL").build());
        mobilePreRenderEnabler = false;
        endDate = new Date();
        startDate = new Date();
        endDate = addHour(1, endDate);
    }

    public Date addHour(int h, Date d) {
        Calendar cal = Calendar.getInstance(); // creates calendar
        cal.setTime(d); // sets calendar time/date
        cal.add(Calendar.HOUR_OF_DAY, h); // adds one hour
        return cal.getTime();

    }

    public StreamedContent getFileDM() {
       serverLog("plik pobieram");
        return fileDM;
    }

    public void setFileDM(StreamedContent fileDM) {
        this.fileDM = fileDM;
    }

    public File getFileToDownloadM() {
        return fileToDownloadM;
    }

    public void setFileToDownloadM(File fileToDownloadM) {
        this.fileToDownloadM = fileToDownloadM;
    }

    public Integer getUserID() {
        if (this.lg == null) {
            FacesContext context = FacesContext.getCurrentInstance();
            Application app = context.getApplication();
            this.lg = (LoginBean) app.evaluateExpressionGet(context, "#{loginBean}", LoginBean.class);
        }
        return this.lg.getId();
    }

    public Integer getChoosenCarId() {
        return choosenCarId;
    }

    public String getEndDateForUser() {
        return endDateForUser;
    }

    public void setEndDateForUser(String endDateForUser) {
        this.endDateForUser = endDateForUser;
    }

    public boolean isIsSucces() {
        return isSucces;
    }

    public void setIsSucces(boolean isSucces) {
        this.isSucces = isSucces;
    }

    public String getSpaceType() {
        return spaceType;
    }

    public void setSpaceType(String spaceType) {
        serverLog(spaceType);
        this.spaceType = spaceType;
        setSpaceTypeForUser(spaceTypes.All.fromValueToLabel(this.spaceType));
        getAvalivableSpaces();
        serverLog("liczba rozwiazań : " + this.avalivableSpaces.size());

    }

    public String getSpaceTypeForUser() {
        return spaceTypeForUser;
    }

    public void setSpaceTypeForUser(String spaceTypeForUser) {
        this.spaceTypeForUser = spaceTypeForUser;
    }

    public void setChoosenCarId(Integer choosenCarId) {
        this.choosenCarId = choosenCarId;
    }

    public UserWizard() {
        Logger.getLogger(UserWizard.class.getName()).log(Level.SEVERE, "create new");
    }

    public ClientsModel getCurrentUser() {
        return currentUser;
    }

    public DAO getSingleDao() {
        if (this.dso == null) {
            FacesContext context = FacesContext.getCurrentInstance();
            Application app = context.getApplication();
            this.dso = (DaoSingleObject) app.evaluateExpressionGet(context, "#{daoSingleObject}", DaoSingleObject.class);
        }
        return this.dso.dao;
    }

    public String getStartDateForUser() {
        return startDateForUser;
    }

    public void setStartDateForUser(String startDateForUser) {
        this.startDateForUser = startDateForUser;
    }

    public void setCurrentUser(ClientsModel currentUser) {
        this.currentUser = currentUser;
    }

    public LoginBean getLg() {
        return lg;
    }

    public boolean isMobilePreRenderEnabler() {
        return mobilePreRenderEnabler;
    }

    public void setMobilePreRenderEnabler(boolean mobilePreRenderEnabler) {
        serverLog("ustawia na wlacznik " + mobilePreRenderEnabler);
        this.mobilePreRenderEnabler = mobilePreRenderEnabler;
    }

    public void setLg(LoginBean lg) {
        this.lg = lg;
    }

    public String getAccPayerType() {
        return accPayerType;
    }

    public String getSetAccMobile() {
        return AccMobile;
    }

    public String getAccMobile() {
        return AccMobile;
    }

    public void setAccMobile(String AccMobile) {
        this.radioOptionsACC = AccMobile;
        serverLog("ustawia acc mobile");
        if (mobilePreRenderEnabler) {
            setChosenValues();
        }

        this.AccMobile = AccMobile;
    }

    public String getRadioOptionsACC_mobile() {
        return radioOptionsACC_mobile;
    }

    public void setRadioOptionsACC_mobile(String radioOptionsACC_mobile) {
        this.radioOptionsACC_mobile = radioOptionsACC_mobile;
    }

    public void setAccPayerType(String accPayerType) {
        this.accPayerType = accPayerType;
    }

    public CarsModel getAddedCar() {
        return addedCar;
    }

    public String getCarTypeForUser() {

        if (this.addedCar != null) {
            return carTypes.Motorcycle.fromValueToLabel(this.addedCar.getCarType());
        } else {
            return "brak";
        }
    }

    public void setCarTypeForUser(String carTypeForUser) {
        this.carTypeForUser = carTypeForUser;
    }

    public String getPayerTypeForUser() {
        this.payerTypeForUser = Payer.PRIVATE_PERSON.fromValueToLabel(this.addedAccountingData.getPayerType());
        return payerTypeForUser;
    }

    public void setPayerTypeForUser(String payerTypeForUser) {
        this.payerTypeForUser = payerTypeForUser;
    }

    public void setAddedCar(CarsModel addedCar) {
        this.addedCar = addedCar;
    }

    public String getSpaceLabelFromValue(ParkingspacesModel ps) {

        return spaceTypes.All.fromValueToLabel(ps.getType());
    }

    public AccountingdataModel getAddedAccountingData() {
        return addedAccountingData;
    }

    public void setAddedAccountingData(AccountingdataModel addedAccountingData) {
        this.addedAccountingData = addedAccountingData;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        logger.log(Level.SEVERE, startDate.toString());
        this.startDate = startDate;
        this.startDateForUser = formatter.format(startDate);

    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        logger.log(Level.SEVERE, endDate.toString());
        this.endDate = endDate;
        this.endDateForUser = formatter.format(endDate);
    }

    public static Logger getLogger() {
        return logger;
    }

    public static void setLogger(Logger logger) {
        UserWizard.logger = logger;
    }

    public String getCarType() {
        String result = carTypes.Motorcycle.fromValueToLabel(carType);
        return carType;
    }

    public void setCarType(String carType) {
        serverLog(carType);
        this.carType = carType;
        setCarTypeForUser(carTypes.Motorcycle.fromValueToLabel(this.carType));

    }

    public List<ParkingspacesModel> getAvalivableSpaces() {
        serverLog("pobieram liste");

        this.avalivableSpaces = dao.getParkingSpacesForReservation(startDate, endDate, spaceType);

        //  logger.log(Level.SEVERE, avalivableSpaces.toString());
        return this.avalivableSpaces;
    }

    public boolean isTakeDataFromClientTable() {
        return takeDataFromClientTable;
    }

    public void setTakeDataFromClientTable(boolean takeDataFromClientTable) {
        this.takeDataFromClientTable = takeDataFromClientTable;
    }

    public void setAvalivableSpaces(List<ParkingspacesModel> avalivableSpaces) {
        this.avalivableSpaces = avalivableSpaces;
    }

    public ParkingspacesModel getSelectedSpace() {
        return selectedSpace;
    }

    public void setSelectedSpace(ParkingspacesModel selectedSpace) {
        logger.log(Level.SEVERE, "Miejsce " + selectedSpace);

        this.selectedSpace = selectedSpace;
    }

    public void refreshDataTable() {
        Logger.getLogger(UserWizard.class.getName()).log(Level.SEVERE, carType);
        this.avalivableSpaces = dao.getParkingSpacesForReservation(startDate, endDate, carType);
    }

    public AccountingdataModel getSelectedAcc() {
        return selectedAcc;
    }

    public void setSelectedAcc(AccountingdataModel selectedAcc) {

        this.selectedAcc = selectedAcc;
        if (mobilePreRenderEnabler) {
            setChosenValues();
        }

    }

    public String getAccName() {
        return accName;
    }

    public void setAccName(String accName) {
        this.accName = accName;
    }

    public String getAccCity() {
        return accCity;
    }

    public void setAccCity(String accCity) {
        this.accCity = accCity;
    }

    public String getAccZip() {
        return accZip;
    }

    public void setAccZip(String accZip) {
        this.accZip = accZip;
    }

    public CarsModel getSelectedCar() {
        return selectedCar;
    }

    public void setSelectedCar(CarsModel selectedCar) {
        serverLog("auto: " + selectedCar.toString());
        this.selectedCar = selectedCar;
    }

    public String getCarPlate() {
        return carPlate;
    }

    public void setCarPlate(String carPlate) {
        serverLog("plate: " + carPlate);
        this.carPlate = carPlate;
    }

    public String getCarModel() {
        return carModel;
    }

    public void setCarModel(String carModel) {
        serverLog("model: " + carModel);
        this.carModel = carModel;
    }

    public String getCarMake() {
        return carMake;
    }

    public void setCarMake(String carMake) {
        serverLog("make: " + carMake);
        this.carMake = carMake;
    }

    public String getAccStreet() {
        return accStreet;
    }

    public String getRadioOptions() {
        return radioOptions;
    }

    public void setRadioOptions(String radioOptions) {
        serverLog("ustawia opcje " + radioOptions);
        this.radioOptions = radioOptions;

    }

    public void setAccStreet(String accStreet) {
        serverLog("ustawiam: " + accStreet);
        this.accStreet = accStreet;
    }

    public String getAccHouse() {
        return accHouse;
    }

    public void setAccHouse(String accHouse) {
        serverLog("ustawiam: " + accHouse);
        this.accHouse = accHouse;
    }

    public String getAccLocal() {
        return accLocal;
    }

    public void setAccLocal(String accLocal) {
        serverLog("ustawiam: " + accLocal);
        this.accLocal = accLocal;
    }

    public String getAccNip() {
        return accNip;
    }

    public void setAccNip(String accNip) {
        serverLog("ustawiam: " + accNip);
        this.accNip = accNip;
    }

    public String getAccPhone() {
        return accPhone;
    }

    private SelectItem[] createSelectItemsCarByClient() {
        List<CarsModel> cars = getCarsByClient();
        SelectItem[] options = new SelectItem[cars.size() + 1];

        options[0] = new SelectItem("", "Wybierz");
        int i = 1;
        for (CarsModel cr : cars) {
            options[i] = new SelectItem(cr.getCarId(), cr.getMake() + " " + cr.getModel() + " " + cr.getNumberPlate());
            i++;
        }

        return options;

    }

    public List<CarsModel> getCarsByClient() {
        if (spaceType != null) {
            if (spaceType.equals("Special")) {
                this.carsByClient = dao.getCarsByClientAndType(currentUser, "Personal");
                this.carsByClient.addAll(dao.getCarsByClientAndType(currentUser, "Trailer"));
                this.carsByClient.addAll(dao.getCarsByClientAndType(currentUser, "Motorcycle"));
                this.carsByClient.addAll(dao.getCarsByClientAndType(currentUser, "Van"));
            } else {
                this.carsByClient = dao.getCarsByClientAndType(currentUser, spaceType);
            }
        }
        return this.carsByClient;
    }

    public void setCarsByClient(List<CarsModel> carsByClient) {
        this.carsByClient = carsByClient;
    }

    public void setAccPhone(String accPhone) {
        serverLog("ustawiam: " + accPhone);
        this.accPhone = accPhone;
    }

    public String getRadioOptionsACC() {
        return radioOptionsACC;
    }

    public void setRadioOptionsACC(String radioOptionsACC) {
        serverLog("ustawia opcje acc" + radioOptionsACC);
        this.radioOptionsACC = radioOptionsACC;
    }

    public String getPayerType() {
        String result = Payer.COMPANY.fromValueToLabel(addedAccountingData.getPayerType());
        return result;
    }

    public SelectItem[] getCarsByClientOptions() {
        if (this.carsByClientOptions == null) {
            this.carsByClientOptions = createSelectItemsCarByClient();
        }
        return this.carsByClientOptions;
    }

    public void setCarsByClientOptions(SelectItem[] carsByClientOptions) {
        this.carsByClientOptions = carsByClientOptions;
    }

    public void setPayerType(String payerType) {
        serverLog("ustawiam: " + accPayerType);

        this.accPayerType = payerType;

    }

    public void setChosenValues() {
        serverLog("setting chosen values");
        setChosenValuesForCar();
        setChosenValuesForAcc();
    }

    public void setChosenValuesProt() {
        if (mobilePreRenderEnabler) {
            serverLog("setting chosen values");
            setChosenValuesForCar();
            setChosenValuesForAcc();
        }
    }

    public void setChosenValuesForCar() {
        serverLog("ustawia wartosci dla car");
        if (radioOptions.equals("1")) {
            serverLog("setting chosen car ");
            this.addedCar = dao.getCarById(choosenCarId);
        }
        if (radioOptions.equals("2")) {
            serverLog("ustawia wartosci car - tworzy z " + carPlate + " " + carMake + " " + carModel);
            this.addedCar = new CarsModel(carPlate, carMake, carModel, carType);
            setCarTypeForUser(carTypes.Motorcycle.fromValueToLabel(addedCar.getCarType()));
        }
    }

    public void addCar() {
        serverLog("ustawia wartosci dla car");
        if (radioOptions.equals("2")) {
            serverLog("zapisuje nowy samochód do bazy");
            this.addedCar = dao.addCar(addedCar);
        }
    }

    public void setChosenValuesForAcc() {
        serverLog("ustawia wartosci dla acc");
        if (radioOptionsACC.equals("1")) {
            serverLog("wybiera z tabli client");
            this.accName = currentUser.getName() + " " + currentUser.getLastName();
            this.accCity = currentUser.getCity();
            this.accZip = currentUser.getZipCode();
            this.accStreet = currentUser.getStreet();
            this.accHouse = currentUser.getHouseNumber();
            this.accLocal = currentUser.getLocalNumber() + "";
            this.accNip = currentUser.getNipNumber();
            this.accPhone = currentUser.getPhone();
            this.accPayerType = "PrivatePerson";
            this.addedAccountingData = new AccountingdataModel(accName, accCity, accZip, accStreet, accHouse, accNip, accPayerType);
        }
        if (radioOptionsACC.equals("2")) {
            serverLog("wybiera z tabli accountingData");
            this.addedAccountingData = dao.getAccountingDataById(selectedAcc.getAccountingDataId());
        }
        if (radioOptionsACC.equals("3")) {
            serverLog("dodaje nowy");
            this.addedAccountingData = new AccountingdataModel(accName, accCity, accZip, accStreet, accHouse, accLocal, accNip, accPhone, accPayerType);
        }
    }

    void serverLog(String x) {
        Logger.getLogger(UserWizard.class.getName()).log(Level.SEVERE, "[Carpark2013]: " + x);
    }

    public String onFlowProcess(FlowEvent event) {
        logger.info("Current wizard step:" + event.getOldStep());
        logger.info("Next step:" + event.getNewStep());

        if (event.getNewStep().equals("st5")) {
            setChosenValues();
        }
        return event.getNewStep();

    }

    public AccountingdataModel isExistClientDataAsAccounting(AccountingdataModel ad) {
        AccountingdataModel result = null;

        for (AccountingdataModel ac : dao.getAccountingDataByClient(currentUser)) {
            serverLog(ac.toString());
            if (ac.getName().equals(ad.getName())
                    && ac.getCity().equals(ad.getCity())
                    && ac.getStreet().equals(ad.getStreet())
                    && ac.getZipNumber().equals(ad.getZipNumber())
                    && ac.getHouseNumber().equals(ad.getHouseNumber())) {
                result = ac;
            }

        }

        return result;
    }

    public void addAc() {
        serverLog("dodaje acc rozmiar listy " + dao.getAccountingDataByClient(currentUser).size());
        serverLog("radio acc: " + radioOptionsACC);

        if (radioOptionsACC.equals("1")) {
            AccountingdataModel temp = isExistClientDataAsAccounting(addedAccountingData);
            if (temp == null) {
                serverLog("temp null");
                addedAccountingData = dao.addAccountingdata(addedAccountingData);
            } else {
                serverLog("podtawiam jako accounting data stare");
                addedAccountingData = temp;
            }
        }

        if (radioOptionsACC.equals("2")) {
        }
        if (radioOptionsACC.equals("3")) {
            serverLog("dodaje nowe do bazy");
            addedAccountingData = dao.addAccountingdata(addedAccountingData);
        }

    }

    public void addReservation(ActionEvent actionEvent) {

        FacesContext context = FacesContext.getCurrentInstance();
        serverLog("Rozpoczynam rezerwowanie");
        ParkingpassesModel pass = new ParkingpassesModel(startDate, endDate, "Temporary");
        TarrifsModel exampleTarrif = dao.getAllTarrifs().get(0);
        addCar();
        pass.setNumberId(selectedSpace);
        pass.setTarrif(exampleTarrif);
        pass.setCarId(addedCar);
        pass = dao.addPass(pass);
        if (pass == null) {
            serverLog("Nie dodał przepustki ");

            context.addMessage(null, new FacesMessage("Wystąpił błąd", "Nie udało się dodać rezererwacji"));

        } else {
            serverLog("przepustka dodana rozmiar listy " + dao.getAccountingDataByClient(currentUser).size());

            PassesrecordsModel record = new PassesrecordsModel(startDate);
            serverLog("rekord " + record.getAddingDate());
            addAc();
            serverLog("rozmiar listy " + dao.getAccountingDataByClient(currentUser).size());
            record.setClientId(currentUser);
            record.setParkingPassId(pass);
            record.setAccountingDataId(addedAccountingData);
            record = dao.addPassRecord(record);

            if (record == null) {
                serverLog("Nie dodał rekordu ");
                context.addMessage(null, new FacesMessage("Wystąpił błąd", "Nie udało się dodać rezererwacji"));
                serverLog("user: " + currentUser.getLastName());
                serverLog("pass: " + pass.getParkingPassId());
                serverLog("ac: " + addedAccountingData.getName());
            } else {
                pass.getPassesrecordsModelCollection().add(record);
                serverLog("rekord dodany rozmiar listy " + dao.getAccountingDataByClient(currentUser).size());
                double amount = getAmount(startDate, endDate, addedCar.getCarType());
                serverLog("Kwota wyliczona to: " + amount);
                ReceivablesModel rcv = new ReceivablesModel(amount, startDate, endDate);
                rcv.setParkingPassId(pass);
                rcv = dao.addReceivable(rcv);

                if (rcv == null) {
                    context.addMessage(null, new FacesMessage("Wystąpił błąd", "Nie udało się dodać rezererwacji"));
                    serverLog("Nie dodał należności " + record.getAddingDate() + " user: " + currentUser.getLastName() + "pass: " + pass.getParkingPassId() + " ac: " + selectedAcc.getName());
                } else {
                    serverLog("należnosć dodany");
                    try {
                        fileToDownload = Ean18Generator.createPdf("przepustka.pdf", pass.getParkingPassId(), currentUser);
                        try {

                            fileDM = new DefaultStreamedContent(new FileInputStream(fileToDownload), "application/pdf", "przepustka.pdf");
                     
                        } catch (FileNotFoundException ex) {
                            Logger.getLogger(UserWizard.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        fileToDownloadM = Ean18Generator.createPdf("przepustka.pdf", pass.getParkingPassId(), currentUser);
                    } catch (IOException ex) {
                        Logger.getLogger(UserWizard.class.getName()).log(Level.SEVERE, "[CarPark] error during saving pdf pass file", ex);
                    } catch (DocumentException ex) {
                        Logger.getLogger(UserWizard.class.getName()).log(Level.SEVERE, "[CarPark] error during saving pdf pass file", ex);
                    }
                    context.addMessage(null, new FacesMessage("Zapisano pomyślnie", "Możesz przejść do swojego profilu."));
                }

            }
        }
    }

    public StreamedContent getFile() {
        StreamedContent file = null;
        try {
            file = new DefaultStreamedContent(new FileInputStream(fileToDownload), "application/pdf", "przepustka.pdf");
            serverLog("plik pobieram");
        } catch (FileNotFoundException ex) {
            Logger.getLogger(UserWizard.class.getName()).log(Level.SEVERE, null, ex);
        }
        return file;
    }

    public StreamedContent getFileM() {
        StreamedContent fileM = null;
        if (mobilePreRenderEnabler) {
            try {
                fileM = new DefaultStreamedContent(new FileInputStream(fileToDownloadM), "application/force-download", "przepustka.pdf");
                serverLog("plik pobieram");
            } catch (FileNotFoundException ex) {
                Logger.getLogger(UserWizard.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return fileM;
    }

    public double getAmount(Date start, Date end, String carType) {
        int months = 0;
        int twWEEKs = 0;
        int days = 0;
        int hours = 0;
        double am = 0.0;
        long difference = end.getTime() - start.getTime();
        //   System.out.println(difference);
        hours = (int) (difference / 1000) / 3600;
        //   System.out.println(godziny);
        if (hours >= 720) {
            months = (int) (hours / 720);
            hours = hours - (months * 720);
        }
        if (hours >= 168) {
            twWEEKs = (int) (hours / 168);
            hours = hours - (twWEEKs * 168);

        }
        if (hours >= 24) {
            days = (int) (hours / 24);
            hours = hours - (days * 24);
        }

        am = months * dao.getTarrifsByCarTypeAndDT(carType, "Month").get(0).getRank()
                + twWEEKs * dao.getTarrifsByCarTypeAndDT(carType, "Two wee").get(0).getRank()
                + days * dao.getTarrifsByCarTypeAndDT(carType, "Day").get(0).getRank()
                + hours * dao.getTarrifsByCarTypeAndDT(carType, "Hour").get(0).getRank();

        return am;
    }

}
